 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { generateUniqueId } from "utils/utils";

/**
 * 本房间的采集任务
 */
export class HarvestInnerMineDecision<  T extends "creep"="creep"> extends ABaseDecision<T>
{
    // 找不到任务。就新增
    public isExpired(event: KeepEvent, region: string): boolean 
    {
        return true;
        // const room = Game.rooms[event.region];
        // const harvesttask = room.getTaskByType("creep").filter(e=>e.data.type == "harvest");
        
        // return harvesttask.length==0;
    }
    public makeTask(event: KeepEvent, region: string): IDecisionCode<T>|false
    {
        const room = Game.rooms[event.region];
        const innerMine = room.getEntitys("InnerMineEntity");
     
        // 俩矿工俩采集工
        const tasks=[];
        const bodyconfig =  getRoleConfig("mine",room.lordRoom().controller.level);
        const carrybodyconfig =  getRoleConfig("porter",room.lordRoom().controller.level);
        for(const e of innerMine)
        {
            if(e.getNodeMemory().state)
            {
                continue;
            }
             
            const work_amont = Math.min( BodyUtils.getCreepCount(bodyconfig.body,WORK,e.getWorkBodyNum()),e.getNodeMemory().max);
            const carry_amount =  BodyUtils.getCreepCount(carrybodyconfig.body,CARRY,e.getCarryBodyNum() *room.memory.stats.porterRadio);
            const curTask = room.findTaskById<ITask<ICreepTaskData<"harvest">>>( e.GUID(),"creep");
            if(curTask)
            {
                global.Log.Info(`${curTask.id}当前采集任务 绑定数据 ${JSON.stringify(curTask.data.creeps)}`);
                curTask.data.arg = {
                    entityId:e.GUID(),
                }
                if(e.Link)
                {
                    curTask.data.required = [
                        [ ["mine"],work_amont,work_amont],
                    ]
                }
                else 
                {
                    curTask.data.required = [
                        [ ["mine"],1,work_amont],
                        [ ["porter"],1,carry_amount]
                    ]
                }
               
            }
            else
            {
                const task:ITaskDeclare["creep"]={
                    id:  e.GUID(),
                    ttl:-1,
                    priority:priority.A,
                    type:"creep",
                    data:{
                        type:"harvest",
                        arg:{
                            entityId:e.GUID(),
                        },
                        room:e.roomName,
                        creeps:[],
                        body:{},        // 当前统计的body数量
                        cost:50,
                        income:3000/300 *1500 * (work_amont/6.0), // 1500T内的收入
                        required:[
                            [ ["mine"],work_amont,work_amont],
                            [ ["porter"],carry_amount,carry_amount]
                        ]
                    },
                    shard:e.getAroundPos().length,
                }
                tasks.push(task);
            }
            
        }
        if(tasks.length>0)
        {
            const code:IDecisionCode<T>={
                region:event.region,
                task:tasks,
                isunique:false,
            }
            return code
        }
        return false
    }
    
}